package managers;

import java.io.File;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import rules.Action;
import rules.Rule;

public class RuleManager {

	//For extracting info from XML
	public static Rule readRuleFromFile(File xmlFile){
		SAXBuilder builder = new SAXBuilder();
		
		Rule rule = new Rule();
		
		try{
			Document document = (Document) builder.build(xmlFile);
			Element rootNode = document.getRootElement();
			
			rule.setName(rootNode.getAttributeValue("name").toString());
			rule.setGroupName(rootNode.getAttributeValue("group").toString());
			
			Element inputCriteria = rootNode.getChild("matching-criteria");
			for(Element element: inputCriteria.getChildren())
				rule.addCriterion(rule.createCriteria(element));
			
			Element outputActions = rootNode.getChild("output-actions");
			for(Element element: outputActions.getChildren())
				rule.addAction(Action.createInstance(element));
			
			return rule;
			
		}catch(Exception e){e.printStackTrace();}
		
		return null;
	}
}